﻿using System;
using System.Web;
using System.Web.Caching;

namespace Repo.DataCaching
{
    public class InMemoryCache
    {
        public static T GetItems<T>(string key) where T : class
        {
            var cachedItems = HttpRuntime.Cache.Get(key);
            if (cachedItems != null)
            {
                return (T)cachedItems;
            }
            return null;
        }

        public static void SaveItems<T>(string key, T itemToStore, TimeSpan expiry) where T : class
        {
            HttpRuntime.Cache.Add(key, itemToStore, null, DateTime.Now.Add(expiry),
                                  Cache.NoSlidingExpiration, CacheItemPriority.High, null);
        }

        public static bool DeleteItems(string key)
        {
            if(HttpRuntime.Cache.Get(key) != null)
            {
                HttpRuntime.Cache.Remove(key);
                return true;
            }
            return false;
        }
    }
}
